package com.pur.zsms.unusual.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterRealVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.supplier.dao.SupplierOrderDao;
import com.pur.zsms.systemsetting.datadictionary.dao.IDataDictionaryDao;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.unusual.dao.IUnusualDao;
import com.pur.zsms.unusual.po.OrderSupplierMaterUnusualPo;
import com.pur.zsms.unusual.service.IUnusualService;
import com.pur.zsms.unusual.vo.OrderSupplierMaterUnusualVo;

@Service("unusualService")
public class UnusualServiceImpl implements IUnusualService {
	String msg = "";

	@Resource(name = "selectFoodDao")
	private ISelectFoodDao selectFoodDaoImpl;

	@Resource(name = "unusualDao")
	private IUnusualDao unusualDao;

	@Resource(name = "dataDictionaryDao")
	private IDataDictionaryDao dataDictionaryDao;

	@Resource(name = "supplierOrderDao")
	private SupplierOrderDao supplierOrderDao;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public ISelectFoodDao getSelectFoodDaoImpl() {
		return selectFoodDaoImpl;
	}

	public void setSelectFoodDaoImpl(ISelectFoodDao selectFoodDaoImpl) {
		this.selectFoodDaoImpl = selectFoodDaoImpl;
	}

	public SupplierOrderDao getSupplierOrderDao() {
		return supplierOrderDao;
	}

	public void setSupplierOrderDao(SupplierOrderDao supplierOrderDao) {
		this.supplierOrderDao = supplierOrderDao;
	}

	public IDataDictionaryDao getDataDictionaryDao() {
		return dataDictionaryDao;
	}

	public void setDataDictionaryDao(IDataDictionaryDao dataDictionaryDao) {
		this.dataDictionaryDao = dataDictionaryDao;
	}

	public IUnusualDao getUnusualDao() {
		return unusualDao;
	}

	public void setUnusualDao(IUnusualDao unusualDao) {
		this.unusualDao = unusualDao;
	}

	/**
	 * 增加异常实体对象
	 */
	@Override
	public void addOrderSupplierMaterUnusualPo(List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList)
	        throws GenericException {
		for (OrderSupplierMaterRealVo orderSupplierMaterRealVo : orderSupplierMaterRealVoList) {
			if (!"".equals(orderSupplierMaterRealVo.getUnusualRemark())) {
				OrderSupplierMaterUnusualPo tOrderSupplierMaterUnusualPo = new OrderSupplierMaterUnusualPo();
				BeanCopier copier = BeanCopier.create(OrderSupplierMaterRealVo.class,
				        OrderSupplierMaterUnusualPo.class, false);
				copier.copy(orderSupplierMaterRealVo, tOrderSupplierMaterUnusualPo, null);
				tOrderSupplierMaterUnusualPo.setUnusualDealFlag(0);// 异常未修正
				tOrderSupplierMaterUnusualPo.setOrdMaterWeight(orderSupplierMaterRealVo
				        .getOrdMaterWeightReal());
				unusualDao.addOrderSupplierMaterUnusualPo(tOrderSupplierMaterUnusualPo);
			}
		}
	}

	public PaginationSupport findUnusualOrder(PageController pageController,
	        OrderSupplierMaterUnusualVo orderSupplierMaterUnusualVo) throws GenericException {
		return unusualDao.findUnusualOrder(pageController, orderSupplierMaterUnusualVo);
	}

	public PaginationSupport findUnusualDetailed(PageController pageController,
	        OrderSupplierMaterUnusualVo orderSupplierMaterUnusualVo, String oid, String supId)
	        throws GenericException {
		return unusualDao.findUnusualDetailed(pageController, orderSupplierMaterUnusualVo, oid, supId);
	}

	public String getAllUnusualDealJsonString(int space) throws GenericException {
		List<Code> codeList = dataDictionaryDao.findCodeIdList("unusual_deal");
		StringBuffer ql = new StringBuffer();
		ql.append("[");
		if (space == 1) {
			ql.append("{\"unusualDeal\":\"0\",\"unusualDealName\":\"未处理\"},");
		}
		String q2 = "";
		if (codeList != null && codeList.size() > 0) {
			for (Code code : codeList) {
				ql.append("{\"unusualDeal\":\"" + code.getCodeId() + "\",\"unusualDealName\":\""
				        + code.getCodeName() + "\"},");
			}
			q2 = ql.substring(0, ql.length() - 1) + "]";
		}
		return q2;
	}

	/**
	 * 异常订单处理
	 * */
	public void orderUnusualDeal(SelectMaterVo selectMaterVo, int ordId) throws GenericException {
		List<OrderSupplierMaterUnusualPo> tOrderSupplierMaterUnusualPoList = unusualDao
		        .getOrderSupplierMaterUnusualPoList(selectMaterVo.getOrdId(), selectMaterVo.getSupId(),
		                selectMaterVo.getOrdMaterId());

		if (tOrderSupplierMaterUnusualPoList != null && tOrderSupplierMaterUnusualPoList.size() == 1) {
			OrderSupplierMaterUnusualPo tOrderSupplierMaterUnusualPo = tOrderSupplierMaterUnusualPoList
			        .get(0);
			tOrderSupplierMaterUnusualPo.setUnusualDeal(selectMaterVo.getUnusualDeal());
			tOrderSupplierMaterUnusualPo.setUnusualDealDetailed(selectMaterVo.getUnusualDealDetailed());
			tOrderSupplierMaterUnusualPo.setSelfOrdId(ordId);
			tOrderSupplierMaterUnusualPo.setUnusualDealFlag(1);// 是否处理
			unusualDao.addOrderSupplierMaterUnusualPo(tOrderSupplierMaterUnusualPo);
		}

		// 查询是否该供货商订单 是否全部 异常 已经解决，如果解决则改变 总供货商表的异常状态
		selectMaterVo.setOrdSupId(selectMaterVo.getSupId());
		List<Object[]> list = unusualDao.judgeAllUnusual(selectMaterVo.getOrdId(),
		        selectMaterVo.getOrdSupId());
		if (list == null || list.size() == 0) {
			OrderSupplierPo tOrderSupplierPo = supplierOrderDao.getOrderSupByOrdIdSupId(
			        String.valueOf(selectMaterVo.getOrdId()), String.valueOf(selectMaterVo.getOrdSupId()));
			tOrderSupplierPo.setOrdPaymentState(0);
			selectFoodDaoImpl.updateOrderSupplier(tOrderSupplierPo);
		}
	}

	public void orderUnusualDealForReturnGoods(SelectMaterVo selectMaterVo) throws GenericException {
		OrderSupplierMaterRealPo tOrderSupplierMaterRealPo = supplierOrderDao
		        .getOrderSupMaterRealByOrdIdMaterIdSupId(selectMaterVo.getOrdId(),
		                selectMaterVo.getOrdMaterId(), selectMaterVo.getOrdSupId());
		tOrderSupplierMaterRealPo.setReturnGoods(1);// 退货标记
		supplierOrderDao.updateOrderSupplierMaterReal(tOrderSupplierMaterRealPo);

		// 重新计算总价格
		OrderSupplierPo orderSupplierPo = supplierOrderDao.getOrderSupByOrdIdSupId(
		        String.valueOf(selectMaterVo.getOrdId()), String.valueOf(selectMaterVo.getOrdSupId()));

		BigDecimal valueReal = supplierOrderDao.computeValueReal(selectMaterVo.getOrdId(),
		        selectMaterVo.getOrdSupId());
		orderSupplierPo.setOrdValueReal(valueReal);
		selectFoodDaoImpl.updateOrderSupplier(orderSupplierPo);

	}
}
